package com.example.uniapp.activity.userHome;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.uniapp.R;
import com.example.uniapp.dao.UserDao;
import com.example.uniapp.utils.Tools;

public class UserUpdatePwdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_update_pwd);

        //获取各个组件
        Toolbar toolbar = this.findViewById(R.id.user_update_pwd_toolbar);
        TextView oldPwdTextView = this.findViewById(R.id.user_update_pwd_old_pwd);
        TextView newPwdTextView = this.findViewById(R.id.user_update_pwd_new_pwd);
        TextView newRepwdTextView = this.findViewById(R.id.user_update_pwd_new_repwd);
        Button button = this.findViewById(R.id.user_update_pwd_button);

        //获取用户名
        String userId = Tools.getOnAccount(this);

        //设置toolbar返回
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击则返回
                finish();
            }
        });

        //设置点击按钮后提交修改密码
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //先获取新密码和旧密码
                String oldPwd = oldPwdTextView.getText().toString();
                String newPwd = newPwdTextView.getText().toString();
                String newRepwd = newRepwdTextView.getText().toString();

                //判断新旧密码是否为空或两次输入的密码是否一致
                if(oldPwd.isEmpty()) {
                    Toast.makeText(UserUpdatePwdActivity.this, "请输入原密码!", Toast.LENGTH_SHORT).show();
                }else if(newPwd.isEmpty()) {
                    Toast.makeText(UserUpdatePwdActivity.this, "新密码不能为空!", Toast.LENGTH_SHORT).show();
                }else if(newRepwd.isEmpty()) {
                    Toast.makeText(UserUpdatePwdActivity.this, "请再次输入新密码!", Toast.LENGTH_SHORT).show();
                }else if(!newPwd.equals(newRepwd)) {
                    Toast.makeText(UserUpdatePwdActivity.this, "两次输入密码不一致!", Toast.LENGTH_SHORT).show();
                }else if(oldPwd.equals(newPwd)) {
                    Toast.makeText(UserUpdatePwdActivity.this, "新旧密码一致!请确保两次密码不一样", Toast.LENGTH_SHORT).show();
                }else {
                    //到这里说明可以更新数据库
                    int result = UserDao.updateUserPwd(userId, newPwd);
                    if(result == 1) {
                        Toast.makeText(UserUpdatePwdActivity.this, "更新密码完毕", Toast.LENGTH_SHORT).show();
                        finish();
                    }else {
                        Toast.makeText(UserUpdatePwdActivity.this, "更新密码失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}